package com.jcfy.project.module.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jcfy.common.utils.bean.BeanUtils;
import com.jcfy.project.module.domain.TOrderGoods;
import com.jcfy.project.module.service.ITOrderGoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jcfy.framework.aspectj.lang.annotation.Log;
import com.jcfy.framework.aspectj.lang.enums.BusinessType;
import com.jcfy.project.module.domain.TOrder;
import com.jcfy.project.module.service.ITOrderService;
import com.jcfy.framework.web.controller.BaseController;
import com.jcfy.framework.web.domain.AjaxResult;
import com.jcfy.common.utils.poi.ExcelUtil;
import com.jcfy.framework.web.page.TableDataInfo;

/**
 * 订单Controller
 *
 * @author jiangfeng
 * @date 2020-08-05
 */
@RestController
@RequestMapping("/module/order" )
public class TOrderController extends BaseController {
    @Autowired
    private ITOrderService tOrderService;
    @Autowired
    private ITOrderGoodsService tOrderGoodsService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('module:order:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(TOrder tOrder) {
        startPage();
        List<TOrder> list = tOrderService.selectTOrderList(tOrder);
        List<Object> orderList = new ArrayList<>();
        list.forEach(item -> {
            Map<String, Object> orderMap = BeanUtils.bean2Map(item);
            List<TOrderGoods> orderGoodsList = tOrderGoodsService.selectTOrderGoodsList(new TOrderGoods(item.getOrderId(), 0));
            List<Object> cartsList = new ArrayList<>();
            orderGoodsList.forEach(orderGoods -> {
                Map<String, Object> cartsMap = new HashMap<>();
                cartsMap.put("count", orderGoods.getNumber());
                cartsMap.put("goods", orderGoods);
                cartsList.add(cartsMap);
            });
            orderMap.put("carts", cartsList);
            orderList.add(orderMap);
        });
        return getDataTable(orderList);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('module:order:export')" )
    @Log(title = "订单" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(TOrder tOrder) {
        List<TOrder> list = tOrderService.selectTOrderList(tOrder);
        ExcelUtil<TOrder> util = new ExcelUtil<TOrder>(TOrder. class);
        return util.exportExcel(list, "order" );
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:order:query')" )
    @GetMapping(value = "/{orderId}" )
    public AjaxResult getInfo(@PathVariable("orderId" ) Long orderId) {
        return AjaxResult.success(tOrderService.selectTOrderById(orderId));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('module:order:add')" )
    @Log(title = "订单" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TOrder tOrder) {
        return toAjax(tOrderService.insertTOrder(tOrder));
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('module:order:edit')" )
    @Log(title = "订单" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TOrder tOrder) {
        return toAjax(tOrderService.updateTOrder(tOrder));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('module:order:remove')" )
    @Log(title = "订单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}" )
    public AjaxResult remove(@PathVariable Long[] orderIds) {
        return toAjax(tOrderService.deleteTOrderByIds(orderIds));
    }
}
